package cn.yitun.chat.yitunserverfilecore.service.impl;

import cn.yitun.chat.yituncommonutil.YitunMD5Util;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.common.ImageData;
import cn.yitun.chat.yitunservercommonlib.util.FileNameUtil;
import cn.yitun.chat.yitunserverfilecore.component.AcceptFileHandler;
import cn.yitun.chat.yitunserverfilecore.component.data.FileInfo;
import cn.yitun.chat.yitunserverfilecore.dao.ImageDataDao;
import cn.yitun.chat.yitunserverfilecore.manage.FileServerConfigManager;
import cn.yitun.chat.yitunserverfilecore.service.ImageService;
import cn.yitun.chat.yitunserverfilecore.util.FilePathUtil;
import cn.yitun.chat.yitunserverfilecore.util.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * 
 * @author yitun
 * @date 2015-12-30 08:59:57
 * @version 0.0.1
 */
@Service
@Transactional
public class ImageServiceImpl implements ImageService {

	private String upload = "upload";

	@Autowired
	private ImageDataDao commonDAO;

	@Resource
	private FileServerConfigManager fileServerConfigManager;
	
	AcceptFileHandler acceptFileHandler = new AcceptFileHandler();

	public List<ImageData> uploadByIO(HttpServletRequest request, Long userId) {
		String basePath = request.getSession().getServletContext().getRealPath("/") + upload;
		String path =fileServerConfigManager.getSavePath();
		StringBuilder rootPath = new StringBuilder((YitunStringUtil.isEmpty(path)) ? basePath : path);// 拼接文件上传目录
		String nodePath = FilePathUtil.getNodePath("images/");
		List<FileInfo> list = acceptFileHandler.uploadByIO(request, rootPath.toString(), nodePath);
		return add(list, userId);
	}

	private List<ImageData> add(List<FileInfo> fileInfoList, Long userId) {
		List<ImageData> list = new ArrayList<ImageData>();

		if (null != fileInfoList && !fileInfoList.isEmpty()) {
			for (FileInfo fi : fileInfoList) {
				File file = fi.getFile();
				long length = file.length();
				String rootPath = fi.getRootPath();
				String nodePath = fi.getNodePath();
				String saveName = fi.getSaveName();
				String name = fi.getName();
				String suffixName = FileNameUtil.getSuffixName(name);

				ImageData imageData = new ImageData();
				imageData.setName(name);
				imageData.setSaveName(saveName);
				imageData.setSuffixName(suffixName);
				imageData.setUserId(userId);
				imageData.setSize(length);
				imageData.setRootPath(rootPath);
				imageData.setNodePath(nodePath);

				imageData.setUrl("/files/" + nodePath + saveName);
				imageData.setMd5(YitunMD5Util.md5L32(saveName));
				imageData.setType(String.valueOf(FileType.dealFileType(suffixName)));
				imageData.setCreateBy(userId);
				imageData.setUpdateBy(userId);
				commonDAO.save(imageData);

				list.add(imageData);
			}
		}
		return list;
	}

	public ImageData getImageData(Long id) {
		return this.commonDAO.get(id);
	}
}
